package com.qtu.product_service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qtu.entity.BmSpecification;
import com.qtu.product_service.entity.ProductMapper;
import com.qtu.product_service.feign.HomeFeign;
import com.qtu.product_service.mapper.HomeMapper;
import com.qtu.product_service.service.HomeService;
import com.qtu.util.CacheUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class HomeServiceImpl extends ServiceImpl<HomeMapper, BmSpecification> implements HomeService {

    @Autowired
    private HomeMapper homeMapper;

    @Autowired
    private HomeFeign homeFeign;

    private String HotGoods = "HotGoods-";//秒杀商品

    private String NewGoods = "NewGoods-";//推荐商品

    private String GoodsDetail = "GoodsDetail-";//商品详情

    /**
     * 秒杀商品查询
     * @param mid 店铺ID
     * @param count 商品数量/条
     * @param killIds 参与过秒杀活动的商品id
     * @return
     */
    @Transactional
    @Override
    public ConcurrentHashMap<Object, Object> getHotGoods(int mid, int count, String killIds){
        List<ProductMapper> goods = new ArrayList<>();
        if(StringUtils.equals("null",killIds)){
            //1、第一次查询从数据库中取出
            goods = homeMapper.getSecKillGoods(mid, count);
            for (ProductMapper good : goods) {
                homeFeign.set(HotGoods+good.getId(),good);//2、加入进缓存
            }
        }else{//2、从缓存中加载秒杀商品信息
            String[] split = killIds.split(",");
            for (String killId : split) {
                Object obj = homeFeign.get(HotGoods+killId);
                ProductMapper productMapper = (ProductMapper) CacheUtil.linkedHashMapToObject(obj, ProductMapper.class);
                if(productMapper != null){
                    goods.add(productMapper);
                }else {//4、缓存中没有，从数据库中取出
                    goods.clear();//如果缓存取出数据时，中间中断，清空集合，从数据库重新加载！
                    goods = homeMapper.getSecKillGoods(mid, count);
                    for (ProductMapper good : goods) {
                        homeFeign.set(HotGoods+good.getId(),good);//4、加入进缓存
                    }
                }
            }
        }
        ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>();
        map.put("goods",goods);
        map.put("startTime",new SimpleDateFormat("HH:mm").format(System.currentTimeMillis()));
        return map;
    }

    /**
     * 首页：查询推荐商品
     * @param mid 店铺ID
     * @param time 新品推荐：最近/天
     * @param count 商品数量/条
     * @return
     */
    @Override
    public List<ProductMapper> getNewGoods(int mid, int time, int count,String newIds) {
        List<ProductMapper> newGoods = new ArrayList<>();
        if(newIds == null){
            newGoods = homeMapper.getNewGoods(mid, time, count);//1、缓存中没有，从数据库中取出
            for (ProductMapper newGood : newGoods) {
                homeFeign.set(NewGoods+newGood.getId(),newGood);//2、加入进缓存
            }
        }else{//3、从缓存中查询该商品
            String[] split = newIds.split(",");
            for (String s : split) {
                Object obj = homeFeign.get(NewGoods+s);
                ProductMapper productMapper = (ProductMapper) CacheUtil.linkedHashMapToObject(obj, ProductMapper.class);
                if(productMapper != null){//如果缓存取出数据时，中间中断，清空集合，从数据库重新加载！
                    newGoods.add(productMapper);
                }else{
                    newGoods.clear();
                    newGoods = homeMapper.getNewGoods(mid, time, count);//1、缓存中没有，从数据库中取出
                    for (ProductMapper newGood : newGoods) {
                        homeFeign.set(NewGoods+newGood.getId(),newGood);//2、加入进缓存
                    }
                }
            }
        }
        return newGoods;
    }

    @Override
    public List<ProductMapper> getGoodsOne(int goodsId) {
        List<ProductMapper> goods = new ArrayList<>();
        Object obj = homeFeign.get(GoodsDetail+goodsId);//1、从缓存中查询该商品详情
        ProductMapper productMapper = (ProductMapper) CacheUtil.linkedHashMapToObject(obj, ProductMapper.class);
        if(productMapper != null){
            goods.add(productMapper);//2、缓存中存在就取出
        }else{//3、缓存中没有，从数据库中取出
            goods = homeMapper.getGoodsOne(goodsId);
            for (ProductMapper one : goods) {
                homeFeign.set(GoodsDetail+one.getId(),one);//4、加入进缓存
            }
        }
        return goods;
    }
}
